<?php
class Common_model extends DS_Model{
    private $model;
    function __construct(){
        parent::__construct();
        $this->load->helpers('string_helper');
    } 
    //根据条件查询一条信息
    public function onceInfo($where,$select,$table){
    	$res=$this->db->where($where)->select($select)->order_by('id desc')->limit(1)->get($table)->row_array();
    	return $res;
    }
    //根据条件查询一条信息
    public function onceInfoBySort($select,$table){
        $res=$this->db->select($select)->order_by('sort_id desc')->limit(1)->get($table)->row_array();
        return $res;
    }
    //查询总的条数
    public function countLog($where,$table){
    	$count=$this->db->where($where)->from($table)->count_all_results();
    	return $count;
    } 
     //添加
    public function insertInfo($table,$data){
        $res=$this->db->insert($table,$data);
        return $res;
    }  
    //修改 
    public function updateInfo($table,$data,$where){
        $res=$this->db->update($table,$data,$where);
        return $res;
    }
    public function getInfo($where,$table){
        $res=$this->db->where(array('id'=>$where))->select("*")->order_by('id desc')->limit(1)->get($table)->row_array();
        return $res;
    }
    //根据条件查询所有信息
    public function allInfoorder($where,$select,$order_by,$table){
        $res=$this->db->where($where)->select($select)->order_by($order_by)->get($table)->result_array();
        return $res;
    }
    public function pageinfotoorder($where,$select,$start,$per,$order_by,$table){
        $res=$this->db->where($where)->select($select)->order_by($order_by)->limit($per,$start)->get($table)->result_array();
        return $res;
    }
    //删除
    public function deleteInfo($table,$where){
        $res=$this->db->delete($table, $where); 
        return $res;
    }
    //压缩图片
    function imgResize($filename)
    {
        $this->load->library('image_lib');
        $arr=@getimagesize($filename);
        $width=$arr[0];
        $height=$arr[1];
        /*   压缩大小
         $size=filesize($filename)/1000;
        if($size > COMPRESS_SIZE){
        $Multiple=$size/COMPRESS_SIZE;
        $quality=100/$Multiple;
        }
        $width=$width*$quality/100;
        $height=$height*$quality/100;
        */
        if($width>COMPRESS_WIDTH_HEIGHT || $height>COMPRESS_WIDTH_HEIGHT){
            $resize['width'] =COMPRESS_WIDTH_HEIGHT;
            $resize['height'] = COMPRESS_WIDTH_HEIGHT;
        }else{
            $resize['width'] = $width;
            $resize['height'] = $height;
        }
    
        $resize['image_library'] = 'GD2';
        $resize['source_image'] = $filename;
        $resize['maintain_ratio'] = TRUE;
        $resize['master_dim'] = 'auto';
        $resize['quality'] = 100;
    
        $this->image_lib->initialize($resize);
        $this->image_lib->resize();
    
    }
}